package com.tvshows.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.tvshows.domain.Historique;
import com.tvshows.domain.Show;
import com.tvshows.repository.HistoriqueRepository;
import com.tvshows.repository.ShowRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing Show.
 */
@RestController
@RequestMapping("/api")
public class ShowResource {

    private final Logger log = LoggerFactory.getLogger(ShowResource.class);

    @Inject
    private ShowRepository showRepository;

    @Inject
    private HistoriqueRepository historiqueRepository;

    /**
     * POST  /shows -> Create a new show.
     */
    @RequestMapping(value = "/shows",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    public void create(@RequestBody Show show){
        log.debug("REST request to save Show : {}", show);
        showRepository.save(show);

    }

    /**
     * GET  /shows -> get all the shows.
     */
    @RequestMapping(value = "/shows",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    public List<Show> getAll() {
        log.debug("REST request to get all Shows");
        return showRepository.findAll();
    }

    /**
     * GET  /shows/:id -> get the "id" show.
     */
    @RequestMapping(value = "/shows/{id}",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    public ResponseEntity<Show> get(@PathVariable String id) {
        log.debug("REST request to get Show MyShow: {}", id);
        return Optional.ofNullable(showRepository.findOne(id))
            .map(show -> new ResponseEntity<>(
                show,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * DELETE  /shows/:id -> delete the "id" show.
     */
    @RequestMapping(value = "/shows/{id}",
            method = RequestMethod.DELETE,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    public void delete(@PathVariable String id) {
        log.debug("REST request to delete Show : {}", id);
        showRepository.delete(id);
    }



    /**
     * GET  /shows/histo/:id -> get the history of the show.
     */
    @RequestMapping(value = "/shows/histo/{id}",
        method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    public List<Historique> getHistorique(@PathVariable String id) {
        log.debug("REST request to get Show History of : {}", id);

        return  historiqueRepository.findByShow(id);
    }

    /**
     * GET  /shows/:id -> get the "id" show.
     */
    @RequestMapping(value = "/shows/addhisto/{id}",
        method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    public void addHistorique(@PathVariable String id) {
        log.debug("REST request to get Show history : {}", id);
        this.historiqueRepository.add(showRepository.findOne(id));
        log.debug("Show succesfully saved", id);




    }


}
